<?php

include_once '../../__global.php';

class aj_add3rdFollow extends ml_controller{
    
    private $follow_uid;
    private $oRedis;
    
    function initParam() {
        $this->follow_uid = $this->input('uid','P');       //要关注用户uid        
        
    }
    
    function main() {
    
    
        $uid = $this->__visitor['uid'];            //当前登录用户uid
        
        $oFollow = new ml_model_dbFollow();
        $oFans = new ml_model_dbFans();
        //增加redis    关注数
        $this->oRedis = new ml_model_rdsCount();

        $rs = $oFollow->isFollow($uid, $this->follow_uid);
//        if(!$rs) $this->api_output(ML_RCODE_FAIL);
        //    判断是否关注过
        $isFollow = $oFollow->get_data();
        if($isFollow) {
            $this->api_output(ML_RCODE_FAIL,array('msg'=>'已经关注过'),'已经关注过');
            exit;
        }
        // 获取当前登陆者的关注数
        $followCnt = $this->oRedis->getFollowCnt($uid);
        
        if($followCnt >= ML_FOLLOW_MAXNUM) {
            $this->api_output(ML_RCODE_FAIL,array('msg'=>'超过关注上线','is_max'=>true),'超过关注上线');
            
        }
        
        $rs = $oFollow->addFollow($uid, $this->follow_uid);
        
        if(!$rs) {
            $this->api_output(ML_RCODE_FAIL,array(),'关注失败');
        }
        
        $rs = $oFans->addFans($this->follow_uid, $uid);
        if(!$rs) {
            $this->api_output(ML_RCODE_FAIL,array(),'加粉丝失败');
        }
        
            
        $this->oRedis->incrFollowCnt($uid);            //    增加 当前用户的关注数

        $this->oRedis->incrFansCnt($this->follow_uid);    //    增加关注人的粉丝数
        
        $oRds = new ml_model_rdsNotice();
        $oRds->add_notice($this->follow_uid, ml_model_rdsNotice::NTCTYPE_ADDFANS);

        $oRdsE = new ml_model_rdsExtFollow();
        $oRdsE->remove_extFollow($uid, $this->follow_uid);
    
        $oNick=new ml_model_rdsHash();
        $newuid = $oRdsE->get_extFollow($uid, 4, 4);
        if (empty($newuid)) {
            $new = array();
        }else {
        $new['newFriend']['uid'] = $newuid[0];
        $new['newFriend']['nick'] = Tool_string::un_html($oNick->getNickByUid($newuid[0]));
        $new['newFriend']['head'] = ml_tool_picid::uid2portrait($newuid[0]);
        }

        if($rs) { //  成功
            ml_tool_actionlog::add_follow($this->__visitor['uid'] , $this->follow_uid);
            ml_tool_sendMq::add_atnick_follow($this->__visitor['uid'], $this->follow_uid);
            $this->api_output(ML_RCODE_SUCC, $new);
        }else {   //  修改密码失败
            $this->api_output(ML_RCODE_FAIL,array(),'关注失败');
        }
    }
    
    
    
    function checkParam(){
        $this->check_referer();
        $rs = ml_tool_isuid::is_mluid($this->follow_uid);
        if(!$rs) {
            $this->api_output(ML_RCODE_FAIL,'','uid不正确');
        }
    }
}

new aj_add3rdFollow();

